
$(function() {
    initTime(".startTime");
    initTime(".endTime");
    $('.datetimepicker-hours thead tr').html('<th style="width:220px;height:0px;"></th>');
    $('.datetimepicker-minutes thead  tr').html('<th class="switch" colspan="5" style="width:220px;">返回</th>');
    //jquery插件jquery.uniform.min.js
    $("input[type='file']").uniform();
    $("input[type='file']").each(function() {
        $(this).next().text($(this).attr("oldBell"));
    });
    $(".press_btn1.uploadbtn").click(function() {
        voiceUpload(this);
    });

    $("#save").click(function() {
        saveAlarmActions();
    });
})

function resetBell(actionId) {
    var oldBell = $("#bellFile"+actionId).attr("oldBell");
    var defaultBell = $("#bellFile"+actionId).attr("defaultBell");
    if (oldBell == defaultBell){
        layer.msg("该文件已经是默认文件，无需更改", {time: 1500});
        return;
    }
    $.ajax({
        url : basePath + "/radar/alarmControl/resetBell.do",
        type : "post",
        dataType : "json",
        data : {
            "actionId" : actionId,
            "defaultBell" : defaultBell
        },
        success : function (json) {
            var code = json.code;
            if (code == "200"){
                layer.msg("重置成功", {time: 1000}, function () {
                    location.reload();
                });
            }else{
                layer.msg("重置失败", {time: 1000});
            }
        }
    })
}

//保存配置后的告警动作
function saveAlarmActions() {
    var alarmActions = [];
    $("div[id^='group']").each(function() {
        var  alarmAction = {};
        alarmAction.alarmActionId = $(this).attr("alarmActionId");
        alarmAction.mail = $(this).find(".mail").get(0).checked;
        alarmAction.message = $(this).find(".message").get(0).checked;
        alarmAction.messageStartTime = $(this).find(".startTime").val();
        alarmAction.messageEndTime = $(this).find(".endTime").val();
        alarmAction.voice = $(this).find(".voice").get(0).checked;
        alarmAction.hint = $(this).find(".hint").get(0).checked;
        alarmActions.push(alarmAction);
    });
    var alarmActionsJsonStr = JSON.stringify(alarmActions);
    $.ajax({
        url:basePath + "/radar/alarmControl/saveAlarmActions.do",
        type:"POST",
        dataType:"json",
        data:{
            "alarmActionsJsonStr":alarmActionsJsonStr
        },
        success:function(json){
            var code = json.code;
            if (code == "200") {
                layer.msg("保存成功!", {time:2000},function(){
                    location.reload();
                });
            }else{
                layer.alert("保存失败!");
            }
        }
    });
}

//上传声音文件
function voiceUpload(btn) {
    var fileId = $(btn).attr("fileId");
    var actionId = $(btn).attr("actionId");
    var oldFileName = $("#" + fileId).attr("oldBell");
    var newFileName = $("#" + fileId).next().text().trim();
    if (oldFileName.trim() == newFileName) {
        layer.msg("未选择文件或与新文件与源文件相同", {time: 2000});
        return;
    }
    //对上传文件类型做判断
    var point = newFileName.lastIndexOf(".");
    var type = newFileName.substring(point, newFileName.length);
    if (type != ".wav" && type != ".mp3") {
        layer.msg("请选择音频文件上传！", {time:2000});
        return;
    }
    $.ajaxFileUpload({ //jquery插件ajaxfileupload.js
        url: basePath + "/radar/alarmControl/voiceUpload.do?actionId=" + actionId,
        type: "post",
        dataType: "json",
        fileElementId : fileId, //上传文件的id，name属性
        secureuri : false,
        success : function(json) {
            var code = json.code;
            if (code == "200"){
                layer.msg("上传成功！", {time:2000}, function () {
                    location.reload();
                });

            }else{
                layer.msg("上传失败!");
            }
        },
        error : function() {
            layer.msg("发送失败!", {
                icon : 2
            });
        }
    });
}

//格式化时间
function initTime(id) {
    $(id).datetimepicker({
        dateFormat:'hh:ii:ss',
        startView:1,
        minuteStep: 30,
        startDate:'00:00:00',
        endDate:'23:30:00',
        autoclose: true
    });
}